package com.baidu.ai.aip.utils;

import com.com.baidu.ai.aip.AI;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;


/***
 * 提取接口中需要的信息
 * 24.9657b8043319a037d3cb96ae933c3c42.2592000.1639986271.282335-25201686
 */
public class GetName {
    public static final int ZERO = 0;
    public static final String NAME = "name";
    public static final String SCORE = "score";
   /* public static void main(String[] args) {
        //获取网络回馈的json
        String zw= AI.plant("web/bs.jpg");
       String dw = AI.animal("web/gb.jpg");
       //提取字符
        Object z = GetName.name(zw);
       Object d = GetName.name(dw);
       //查看默认最大概率信息
        GetName.JJ(zw);
        GetName.JJ(dw);
       //仅要名字或概率，有返回值
        GetName.JJ(zw,0,"name");
        GetName.JJ(dw,1,"score");
    }*/
    /**
     * 通过json--->java进行查看（无返回值），需依赖第三方包。
     */
    public static void JJ(String sj){
       // String sj = "{\"result\":[{\"name\":\"榕树\",\"score\":0.3974799},{\"name\":\"黄杨\",\"score\":0.22109807},{\"name\":\"象牙树\",\"score\":0.17375246}],\"log_id\":1463126565514814703}";
       // String sj ="{\"result\":[{\"name\":\"松鼠\",\"score\":\"0.971857\"},{\"name\":\"红松鼠\",\"score\":\"0.0206066\"},{\"name\":\"灰鼠\",\"score\":\"0.00193363\"},{\"name\":\"岩松鼠\",\"score\":\"0.00190586\"},{\"name\":\"赤腹松鼠\",\"score\":\"0.00052591\"},{\"name\":\"花栗鼠\",\"score\":\"0.000473275\"}],\"log_id\":1463141747397246897}";
        JSONObject jsonObject = JSONObject.fromObject(sj);
        Object o = jsonObject.get("result");
        //System.out.println(jsonObject+":key:result="+o);
        JSONArray jsonArray = JSONArray.fromObject(o);
        Object o1 = jsonArray.get(0);
        //System.out.println(jsonArray+":key:1="+o1);
        JSONObject jsonObject1 = JSONObject.fromObject(o1);
        Object name = jsonObject1.get("name");
        Object score = jsonObject1.get("score");
        //由于此处可能存在强转失败所以要备用一套方案；try catch 没有if效率高
       /* try{
            double score = (double)jsonObject1.get("score");
            System.out.println("识别出概率最高的图片内容为："+name+",且概率为:"+score*100+"%");
        }catch (Exception e){
            String s = (String)jsonObject1.get("score");
            double score = Double.parseDouble(s);
            System.out.println("识别出概率最高的图片内容为："+name+",且概率为:"+score*100+"%");
        }*/
        if (score instanceof Double){
            System.out.println("识别出概率最高的图片内容为："+name+",且概率为:"+(double)score*100+"%");
        }else {
            double s = Double.parseDouble((String) score);
            System.out.println("识别出概率最高的图片内容为：" + name + ",且概率为:" + s* 100 + "%");
        }
    }
    /***
     * 三参数获取有效信息"方法默认提取result中的信息，log_id请重写方法"
     * 基于上面jj()一参方法，此方法有返回值
     * @param sj json
     * @param index 坐标
     * @param key 密钥
     * @return Object(double 或 String)
     */
    public static Object JJ(String sj,Integer index,String key){
        // String sj = "{\"result\":[{\"name\":\"榕树\",\"score\":0.3974799},{\"name\":\"黄杨\",\"score\":0.22109807},{\"name\":\"象牙树\",\"score\":0.17375246}],\"log_id\":1463126565514814703}";
        // String sj ="{\"result\":[{\"name\":\"松鼠\",\"score\":\"0.971857\"},{\"name\":\"红松鼠\",\"score\":\"0.0206066\"},{\"name\":\"灰鼠\",\"score\":\"0.00193363\"},{\"name\":\"岩松鼠\",\"score\":\"0.00190586\"},{\"name\":\"赤腹松鼠\",\"score\":\"0.00052591\"},{\"name\":\"花栗鼠\",\"score\":\"0.000473275\"}],\"log_id\":1463141747397246897}";
        JSONObject jsonObject = JSONObject.fromObject(sj);
        Object o = jsonObject.get("result");
        //System.out.println(jsonObject+":key:result="+o);
        JSONArray jsonArray = JSONArray.fromObject(o);
        if (index<jsonArray.size()){
        Object o1 = jsonArray.get(index);
        //System.out.println(jsonArray+":key:1="+o1);
        JSONObject jsonObject1 = JSONObject.fromObject(o1);
        if (key.equals("name")){
            Object name = jsonObject1.get("name");
            System.out.println("获取的名字内容为："+name);
            return name;
        }else if (key.equals("score")){
            Object score = jsonObject1.get("score");
            //由于此处可能存在强转失败所以要备用一套方案；try catch 没有if效率高
            if (score instanceof Double){
                System.out.println("获取的概率为:"+(double)score*100+"%");
                return score;
            }else {
                double s = Double.parseDouble((String) score);
                System.out.println( "获取的概率为:" + s* 100 + "%");
                return s;
            }
        }
        }
        return "你所查询的内容可能有误,请合适:json："+sj+"子坐标："+index+"密钥："+key;
    }
    /**
     * 针对字符串的位置进行有效提取
     * @param a json
     * @return 两位的名字
     */
    public static Object name(String a){
        String b = (String) a.subSequence(20,22);
        System.out.println(b);
        return b;
    }
    }

